﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        public bool SaveNavigation(Navigation navigation)
        {
            using (CoreContext context = _contextFactory.Get())
            {
                if (ParameterUtility.IsNull(navigation.ParentId))
                {
                    navigation.ParentId = null;
                }

                if (context.Set<Navigation>()
                    .Where(p => p.Id == navigation.Id)
                    .Select(p => new { p.Id })
                    .FirstOrDefault() == null)
                {
                    context.Set<Navigation>().Add(navigation);

                    if (!ParameterUtility.IsNull(navigation.PageId))
                    {
                        Logger.Info(string.Format("add privilege for page navigation {0}({1})", navigation.Caption, navigation.Id));

                        Privilege privilege = new Privilege();
                        privilege.Id = Guid.NewGuid();
                        privilege.AppId = navigation.AppId;
                        privilege.Name = string.Format("NAVIGATION_PAGE_{0}", navigation.Id.ToString().ToUpper());
                        privilege.SourceId = navigation.Id;
                        privilege.Type = "NAVGATION_PAGE";
                        privilege.Caption = navigation.Caption;
                        privilege.Description = navigation.Description;
                        EntityRepository.Save(privilege, context.DbContext);
                    }
                    else
                    {
                        Logger.Info(string.Format("add privilege for app navigation {0}({1})", navigation.Caption, navigation.Id));

                        Privilege privilege = new Privilege();
                        privilege.Id = Guid.NewGuid();
                        privilege.AppId = navigation.AppId;
                        privilege.Name = string.Format("NAVIGATION_APP_{0}", navigation.Id.ToString().ToUpper());
                        privilege.SourceId = navigation.Id;
                        privilege.Type = "NAVGATION_APP";
                        privilege.Caption = navigation.Caption;
                        privilege.Description = navigation.Description;
                        EntityRepository.Save(privilege, context.DbContext);
                    }
                }
                else
                {
                    context.Entry(navigation).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }


        public Navigation GetDefaultNavigation(Guid applicationId)
        {
            //using (CoreContext context = _contextFactory.Get())
            //{
            //    return context.Set<Navigation>()
            //        .Where(p => p.ApplicationId == applicationId && p.IsDefault == true)
            //        .FirstOrDefault();
            //}
            return null;
        }

        public ListResult<Navigation> GetNavigations(Guid appId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                List<Navigation> roots = context.Set<Navigation>().Where(p => p.AppId == appId && (p.PageId == null || p.PageId.Value == default(Guid))).ToList();

                Stack<Navigation> stack = new Stack<Navigation>();
                foreach (Navigation root in roots)
                {
                    stack.Push(root);
                }
                while (stack.Count > 0)
                {
                    Navigation item = stack.Pop();
                    item.Children = context.Set<Navigation>()
                        .Where(p => p.ParentId == item.Id)
                        .OrderBy(p => p.SortIndex)
                        .ToList();

                    if (item.Children != null)
                    {
                        foreach (Navigation child in item.Children)
                        {
                            stack.Push(child);
                        }
                    }
                }
                return new ListResult<Navigation>(roots);
            }
        }

        public ListResult<Navigation> GetNavigations(Guid appId, Guid pageId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                List<Navigation> roots = context.Set<Navigation>().Where(p => p.AppId == appId && p.PageId.Value == pageId).ToList();

                Stack<Navigation> stack = new Stack<Navigation>();
                foreach (Navigation root in roots)
                {
                    stack.Push(root);
                }
                while (stack.Count > 0)
                {
                    Navigation item = stack.Pop();
                    item.Children = context.Set<Navigation>()
                        .Where(p => p.ParentId == item.Id)
                        .OrderBy(p => p.SortIndex)
                        .ToList();

                    if (item.Children != null)
                    {
                        foreach (Navigation child in item.Children)
                        {
                            stack.Push(child);
                        }
                    }
                }
                return new ListResult<Navigation>(roots);
            }
        }
    }
}
